import Toast from '@vant/weapp/toast/toast';
const app = getApp()

const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatarUrl: defaultAvatarUrl,
    theme: wx.getSystemInfoSync().theme,
    nickname:''
  },
  onLoad() {
    wx.onThemeChange((result) => {
      this.setData({
        theme: result.theme
      })
    })
  },
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail 
    this.setData({
      avatarUrl,
    })
  },
  getNickName(e){
    this.setData({
      nickname:e.detail.value
    })
  },
  confirm(){
    const that = this
    var avatarUrl = this.data.avatarUrl
    if(this.data.avatarUrl===''||this.data.nickname===''){
      Toast('请选择头像和昵称!')
      return
    }
    wx.setStorageSync('nickName', this.data.nickname)
    
    wx.uploadFile({
      url: 'https://zhixiao.site:8080/avatar/upload', // 仅为示例，非真实的接口地址
      filePath: avatarUrl,
      name: 'file',
      formData: { user: 'test' },
      success:(res)=>{
      },
      //设置头像和昵称
      complete:(res)=>{
        console.log(res)
        avatarUrl = res.data.slice(32,res.data.length-3)
        wx.setStorageSync('avatarUrl', avatarUrl)
        console.log(avatarUrl)
        wx.request({
          url: 'https://zhixiao.site:8080/client/setMsg',
          data:{
            userId:wx.getStorageSync('userid'),
            avatarUrl:avatarUrl,
            nickName:that.data.nickname
          },
          success:(res)=>{
            console.log(res)
          }
        })
      }
    });
    wx.redirectTo({
      url: '/subpkg/setName/setName',
    })
    // var identity = wx.getStorageSync('identity')
    // if(identity==='老人'){
    //   wx.navigateTo({
    //     url: '/elder/service/service',
    //   })
    // }else if(identity==='护工'){
    //   wx.navigateTo({
    //     url: '/pages/elderList/elderList',
    //   })
    // }else{
    //   wx.switchTab({
    //     url: '/pages/home/home',
    //   })
    // }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})